//
//  HMShoppingCar.m
//  HowToDo
//
//  Created by 宋轩 on 17/1/6.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMShoppingCarTool.h"

@implementation HMShoppingCarTool

+ (instancetype)sharedShoppingCarTool
{
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HMShoppingCarTool alloc] init];
    });
    return instance;
}

- (NSMutableArray *)modelArray
{
    if (_modelArray == nil)
    {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

//- (void)getShoppingCarDataWithModel:(HMShoppingCarModel *)shoppingCarModel
//{
//    HMShoppingCarModel *shoppingCarModel1 = [[HMShoppingCarModel alloc] init];
//    shoppingCarModel1 = shoppingCarModel;
//    [self.modelArray addObject:shoppingCarModel1];
//    [self saveToCaches];
//    [self createNotification];
//}

//购物
- (void)getShoppingCarDataWithID:(NSString *)resource_id  andImageUrl:(NSString *)imageUrl andName:(NSString *)name andContent:(NSString *)content andEnclosure:(NSString *)enclosure andPrice:(NSString *)price
{
    HMShoppingCarModel *shoppingCarModel = [[HMShoppingCarModel alloc] init];
    [shoppingCarModel setValue:resource_id forKey:@"resource_id"];
    [shoppingCarModel setValue:imageUrl forKey:@"image_url"];
    [shoppingCarModel setValue:name forKey:@"resource_name"];
    [shoppingCarModel setValue:content forKey:@"resource_content"];
    [shoppingCarModel setValue:enclosure forKey:@"resource_enclosure"];
    [shoppingCarModel setValue:price forKey:@"price"];

    [self.modelArray addObject:shoppingCarModel];
    [self saveToCaches];
    [self createNotification];
    
}

//取消购物
- (void)removeShoppingCarDataWithID:(NSString *) resource_id
{
    [self.modelArray enumerateObjectsUsingBlock:^(HMShoppingCarModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.resource_id == resource_id)
        {
            [self.modelArray removeObject:obj];
        }
    }];
    
    [self saveToCaches];
    [self createNotification];
}

//保存到沙盒
- (void)saveToCaches
{
    NSString *cachesStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
//    NSString *fileName = [cachesStr stringByAppendingString:@"content.plist"];
    NSString *fileName = [cachesStr stringByAppendingPathComponent:@"content.plist"];
    
    [NSKeyedArchiver archiveRootObject:self.modelArray toFile:fileName];
}

//发送通知
- (void)createNotification
{
    NSString *cachesStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *fileName = [cachesStr stringByAppendingPathComponent:@"content.plist"];
    
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"shoppingCarCount" object:@(arr.count)];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"shoppingCarCount" object:nil userInfo:@{@"shoopingCartArr":arr}];

}

//返回购物车数量
- (NSInteger)getShoppingCarCount
{
    NSString *cachesStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *fileName = [cachesStr stringByAppendingPathComponent:@"content.plist"];
    
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
    
    return arr.count;
}

@end
